/*
 * Copyright (c) 2013. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.jspx.txweb.dao.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jspx.sober.Criteria;
import com.jspx.sober.criteria.expression.Expression;
import com.jspx.sober.criteria.projection.Projections;
import com.jspx.sober.jdbc.JdbcOperations;
import com.jspx.sober.ssql.SSqlExpression;
import com.jspx.txweb.dao.ActionLogDAO;
import com.jspx.txweb.table.ActionLog;
import com.jspx.utils.ArrayUtil;
import com.jspx.utils.DateUtil;
import com.jspx.utils.StringUtil;
import org.slf4j.helpers.MessageFormatter;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: yuan
 * Date: 13-10-16
 * Time: 下午10:13
 *
 *
 */
public class ActionLogDAOImpl extends JdbcOperations implements ActionLogDAO {

    final private static Logger log = LoggerFactory.getLogger(ActionLogDAOImpl.class);

    public ActionLogDAOImpl()
    {

    }

    private String namespace = StringUtil.empty;
    public void setNamespace(String namespace)
    {
        this.namespace = namespace;
    }

    public String getNamespace()
    {
        return namespace;
    }

    /**
     *
     * @param id  id
     * @return 载入
     * @throws Exception
     */
    public Object load(Long id) throws Exception
    {
        return super.load(ActionLog.class,id);
    }
    /**
     *
     *
     * @param ids id 列表
     * @return 更新排序是否成功
     */
    public boolean delete(Long[] ids)
    {
        if (ArrayUtil.isEmpty(ids)) return false;
        try
        {
            for (Long id : ids)
            {
                Object obj =  get(ActionLog.class, id);
                if (obj != null) delete(obj);
            }
        } catch (Exception e)
        {
            log.error(MessageFormatter.format("delete ActionLog {}", ArrayUtil.toString(ids,StringUtil.SEMICOLON)).getMessage(),e);
            return false;
        }
        return true;
    }

    public int clear()
    {
        return createCriteria(ActionLog.class).add(Expression.eq("namespace", namespace)).delete(false);
    }

    /**
     * @param find       查询字符串 "" 自动分开查询
     * @param sortString 排序字符串
     * @param term       特别情况标记  0 不管   1 查询有图片的 2 查询无图片的  3 查询有下载文件的   4 查询无下载文件的  5 查询有图片的
     * @param page      页数
     * @param count      返回数量
     * @return List<HtmlContent>
     */
    @SuppressWarnings("unchecked")
    public List getList(String[] field,String[] find,String term,String sortString,long uid,int page, int count) throws Exception
    {
        if (StringUtil.isNULL(sortString)) sortString="createDate:D";
        Criteria criteria = createCriteria(ActionLog.class).add(Expression.eq("namespace", namespace));
        if (!ArrayUtil.isEmpty(find)&& !ArrayUtil.isEmpty(field)) {
            criteria = criteria.add(Expression.find(field, find));
        }
        if (uid>0)
        {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        return SSqlExpression.getSortOrder(criteria, sortString).setCurrentPage(page).setTotalCount(count).list(false);
    }

    /**
     *
     *
     * @param find  条件
     * @return int 得到记录条数
     */
    public int getCount(String[] field,String[] find,String term,long uid) throws Exception
    {
        Criteria criteria = createCriteria(ActionLog.class).add(Expression.eq("namespace", namespace));
        if (!ArrayUtil.isEmpty(find)&& !ArrayUtil.isEmpty(field)) {
            criteria = criteria.add(Expression.find(field,find));
        }
        if (uid>0)
        {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        return criteria.setProjection(Projections.rowCount()).intUniqueResult();
    }

    public int deleteYearBefore(int year) throws Exception
    {
        return createCriteria(ActionLog.class).add(Expression.lt("createDate", DateUtil.addYear(-year))).delete(false);

    }

}